package com.jevons.geo_edit.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jevons.geo_edit.R;


/**
 * Copyright 2020 JxGIS
 *
 * @file GisGeometryTypeView
 * @auth linaisheng
 * Created on 2023/6/12.
 * Description：
 */
public class GeometryTypeView extends RelativeLayout {

    public static final int GEOMETRY_TYPE_POINT = 0;
    public static final int GEOMETRY_TYPE_LINE= 1;
    public static final int GEOMETRY_TYPE_POLYGON = 2;

    private RelativeLayout rootPolygon;
    private RelativeLayout rootLine;
    private RelativeLayout rootPoint;

    private View polygonView;
    private View lineView;
    private ImageView pointView;

    private int geometryType = GEOMETRY_TYPE_POINT;
    private int iconWidth = 30;
    private int iconHeight = 20;

    public GeometryTypeView(Context context) {
        super(context);
        initUI(context, null);
    }

    public GeometryTypeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUI(context, attrs);
    }

    public GeometryTypeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initUI(context, attrs);
    }


    private void initUI(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.view_geometry_type, this, true);
        rootPolygon = findViewById(R.id.view_geometry_polygon_type);
        rootLine = findViewById(R.id.view_geometry_line_type);
        rootPoint = findViewById(R.id.view_geometry_point_type);

        polygonView = findViewById(R.id.view_geometry_polygon_fill_color);
        lineView = findViewById(R.id.view_geometry_line_color);
        pointView = findViewById(R.id.view_geometry_point_icon);

        TypedArray typedArray = null;
        if(attrs != null){
            typedArray = context.obtainStyledAttributes(attrs, R.styleable.GeometryTypeView);

            iconWidth = typedArray.getLayoutDimension(R.styleable.GeometryTypeView_iconWidth, iconWidth);
            iconHeight = typedArray.getLayoutDimension(R.styleable.GeometryTypeView_iconHeight, iconHeight);

            RelativeLayout[] rootViews = { rootPoint, rootLine, rootPolygon };
            for (RelativeLayout rootView : rootViews) {
                LayoutParams params = (LayoutParams) rootView.getLayoutParams();
                params.width = iconWidth;
                params.height = iconHeight;
                rootView.setLayoutParams(params);
            }
            ViewGroup.LayoutParams pointViewLayoutParams = pointView.getLayoutParams();
            pointViewLayoutParams.height = iconHeight;
            pointViewLayoutParams.width = iconHeight;
            pointView.setLayoutParams(pointViewLayoutParams);

            geometryType = typedArray.getInteger(R.styleable.GeometryTypeView_geoType, GEOMETRY_TYPE_POINT);
            changeGeometryType(geometryType);

            typedArray.recycle();
        }

    }

    public void changeGeometryType(int type) {
        if (type == GEOMETRY_TYPE_POINT) {
            geometryType = GEOMETRY_TYPE_POINT;
            rootPoint.setVisibility(VISIBLE);
            rootLine.setVisibility(INVISIBLE);
            rootPolygon.setVisibility(INVISIBLE);
        } else if (type == GEOMETRY_TYPE_LINE) {
            geometryType = GEOMETRY_TYPE_LINE;
            rootPoint.setVisibility(INVISIBLE);
            rootLine.setVisibility(VISIBLE);
            rootPolygon.setVisibility(INVISIBLE);
        } else {
            geometryType = GEOMETRY_TYPE_POLYGON;
            rootPoint.setVisibility(INVISIBLE);
            rootLine.setVisibility(INVISIBLE);
            rootPolygon.setVisibility(VISIBLE);
        }
    }


    public void changePolygonType(String fillColor, String outLineColor) {
        rootPolygon.setBackgroundColor(Color.parseColor(outLineColor));
        polygonView.setBackgroundColor(Color.parseColor(fillColor));
        changeGeometryType(GEOMETRY_TYPE_POLYGON);
    }

    public void changeLineType(String lineColor) {
        lineView.setBackgroundColor(Color.parseColor(lineColor));
        changeGeometryType(GEOMETRY_TYPE_LINE);
    }

    public void changePointType(Bitmap pointBitmap) {
        pointView.setImageBitmap(pointBitmap);
        changeGeometryType(GEOMETRY_TYPE_POINT);
    }


    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}